home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / ssavwin.exe / SSAVEPWD.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-07  |  18.7 KB  |  381 lines

  1. Unit SSavePwd;
  2.  
  3. {****************************************************************************
  4. *****************************************************************************
  5. *                                                                           *
  6. *  SSavePwd.pas:                                                            *
  7. *  Unit for Windows 3.1 Screen Savers. Exports the types and functions      *
  8. *  necessary for password handling.                                         *
  9. *                                                                           *
  10. *  Compiler: BP 7.0                                                         *
  11. *                                                                           *
  12. *  See SSaveDem.pas.                                                        *
  13. *                                                                           *
  14. *****************************************************************************
  15. *                                                                           *
  16. *  Copyright ⌐ 1993 Manfred Keul [100031,12].                               *
  17. *                                                                           *
  18. *  Rev. 0.1   28.3.93   MK  IR                                              *
  19. *  Rev. 0.2    7.5.93   MK  EditNotify: New logic for enabling idc_EnaPWBut *
  20. *                           No DefWndProc in WMActivate                     *
  21. *                                                                           *
  22. *****************************************************************************
  23. ****************************************************************************}
  24.  
  25.  
  26. interface
  27.  
  28. uses Objects, OWindows, ODialogs, Wintypes, Winprocs, Encrypt;
  29.  
  30. {$I savepwrc.inc      contains rc constants }
  31.  
  32. const SaverName: PChar = 'Screen Saver.NoName';  { Default CONTROL.INI heading }
  33.  
  34. {****************************************************************************
  35. *                                                                           *
  36. *                        T P a s s W o r d D i a l o g                      *
  37. *                                                                           *
  38. *  Dialog box to input and enable the password.                             *
  39. *                                                                           *
  40. ****************************************************************************}
  41.  
  42. type
  43.    PPassWordDialog = ^TPassWordDialog;
  44.    TPassWordDialog = object (TDialog)
  45.        procedure SetupWindow; virtual;
  46.        procedure OK (var Msg: TMessage); virtual id_First + id_OK;
  47.        procedure Help (var Msg: TMessage); virtual id_First + idc_HelpBtn;
  48.        procedure EditNotify1 (var Msg: TMessage); virtual id_First + idc_NewPW1;
  49.        procedure EditNotify2 (var Msg: TMessage); virtual id_First + idc_NewPW2;
  50.        procedure EditNotify (var Msg: TMessage); virtual;
  51.      end;
  52.  
  53. {-----------  function returning "ok to close Screen Saver"  ---------------}
  54.  
  55. function PasswordOK (parent: PWindowsObject): boolean;
  56.  
  57. {****************************************************************************
  58. *                                                                           *
  59. *                         I m p l e m e n t a t i o n                       *
  60. *                                                                           *
  61. ****************************************************************************}
  62.  
  63. implementation
  64.  
  65. {$R ssavepwd.res        contains password dialog layouts }
  66.  
  67. {****************************************************************************
  68. *                                                                           *
  69. *                           T S t o p D i a l o g                           *
  70. *                                                                           *
  71. *  Dialog box asking for password when Screen Saver stops.                  *
  72. *                                                                           *
  73. ****************************************************************************}
  74.  
  75. type
  76.    PStopDialog = ^TStopDialog;
  77.    TStopDialog = object (TDialog)
  78.        procedure SetupWindow; virtual;
  79.        procedure OK (var Msg: TMessage); virtual id_First + id_OK;
  80.        procedure WMActivate (var Msg: TMessage);virtual wm_first+WM_ACTIVATE;
  81.      end;
  82.  
  83. {***************************************************************************}
  84.  
  85. const PWProtKeyName   = 'PWProtected';         { Headings in CONTROL.INI }
  86.       PWStringKeyName = 'Password';
  87.       PWStringHeading = 'ScreenSaver';
  88.  
  89.       PWDlgCaption    = 'Change Password';
  90.       HelpCaption     = 'Password Help';
  91.       MsgTxtNotFnd    = 'Message text not found.';
  92.       StrgSize        = 255;
  93.       HTSize          = 3 * StrgSize;
  94.       PWSize          = 20;                   { Password size }
  95.  
  96. {***************************************************************************}
  97.  
  98. var  Text1:           array [0..HTSize] of char;       { general purpose..}
  99.      Text2, Text3:    array [0..StrgSize] of char;     {..text buffers }
  100.  
  101.      CurrentPW:       array [0..PWSize] of char;       { copies of...}
  102.      PWProtected:     word;                            {..CONTROL.INI values }
  103.  
  104.  
  105. {****************************************************************************
  106. *                                                                           *
  107. *                         G e t P r o f i l e                               *
  108. *                                                                           *
  109. *   Reads screen saver entries from CONTROL.INI.                            *
  110. *                                                                           *
  111. *   INPUT:  SaverName = Heading of the private section of the saver in      *
  112. *                    CONTROL.INI (something like: 'Screen Saver.savername') *
  113. *                                                                           *
  114. *   OUTPUT: Prot = 0 if password for this screen saver is disabled,         *
  115. *                  <> 0 otherwise                                           *
  116. *           PW   = Current password (from PWStringHeading entry )           *
  117. *                                                                           *
  118. ****************************************************************************}
  119.  
  120. procedure GetProfile (var Prot: word; PW: PChar);
  121. begin
  122.  
  123. Prot := GetPrivateProfileInt (SaverName, PWProtKeyName, 0, 'CONTROL.INI');
  124.  
  125. GetPrivateProfileString
  126.    (PWStringHeading, PWStringKeyName, '', PW, PWsize+1, 'CONTROL.INI');
  127.  
  128. end;      { GetProfile }
  129.  
  130. {****************************************************************************
  131. *                                                                           *
  132. *                         P a s s w o r d O K                               *
  133. *                                                                           *
  134. *   Asks user to enter password.                                            *
  135. *                                                                           *
  136. *   INPUT:  parent = pointer to parent windows object.                      *
  137. *           SaverName = Heading of the private section of the saver in      *
  138. *                    CONTROL.INI (something like: 'Screen Saver.savername') *
  139. *                                                                           *
  140. *   OUTPUT: PasswordOK = true if entered password = password in CONTROL.INI *
  141. *                            (or if saver currently not password protected) *
  142. *                                                                           *
  143. ****************************************************************************}
  144.  
  145. function PasswordOK (parent: PWindowsObject): boolean;
  146. begin
  147. GetProfile (PWProtected, CurrentPW);
  148. PasswordOK := true;
  149. if ((PWProtected <> 0) and (CurrentPW [0] <> #0)) then
  150.    PasswordOK := Application^.ExecDialog
  151.                  (New (PStopDialog, Init(parent, 'StopDlg'))) = id_OK;
  152.    { TStopDialog returns id_OK only if password was ok }
  153. end; { Password OK }
  154.  
  155. {***************************************************************************}
  156.  
  157. {****************************************************************************
  158. *                                                                           *
  159. *              T S t o p D i a l o g . S e t u p W i n d o w                *
  160. *                                                                           *
  161. *   Limits text length in StopDialog's edit control idc_StopPW.             *
  162. *                                                                           *
  163. ****************************************************************************}
  164.  
  165. procedure TStopDialog.SetupWindow ;
  166. begin
  167. inherited SetupWindow;
  168. SendDlgItemMsg (idc_StopPW, EM_LimitText, PWSize, 0);
  169. end;
  170.  
  171. {****************************************************************************
  172. *                                                                           *
  173. *              T S t o p D i a l o g . W M A c t i v a t e                  *
  174. *                                                                           *
  175. *   Sets focus in StopDialog to idc_StopPW, selects all its text.           *
  176. *                                                                           *
  177. ****************************************************************************}
  178.  
  179. procedure TStopDialog.WMActivate (var Msg: TMessage);
  180. begin
  181. SetFocus (GetItemHandle (idc_StopPW));
  182. SendDlgItemMsg (idc_StopPW, EM_SetSel, 0, $1000000); { $1000000 = end of line }
  183. { DefWndProc (Msg);  => wrong focus if edit control is empty  (Rev. 0.2) }
  184. end;
  185.  
  186. {****************************************************************************
  187. *                                                                           *
  188. *                         T S t o p D i a l o g . O K                       *
  189. *                                                                           *
  190. *   Checks entered password; if ok, quits StopDialog.                       *
  191. *                                                                           *
  192. *   INPUT: CurrentPW = Current password (from CONTROL.INI)                  *
  193. *                                                                           *
  194. ****************************************************************************}
  195.  
  196. procedure TStopDialog.OK (var Msg: TMessage);
  197.  
  198. var  OldPWInp: array [0..PWSize] of char;
  199.  
  200. begin
  201. GetWindowText (GetItemHandle (idc_StopPW), OldPWInp, PWSize+1);  { user input }
  202. EncryptString (OldPWInp);
  203. if (lstrcmpi (OldPWInp, CurrentPW) <> 0) then      {  passwords match ? }
  204.      begin                                         { if not,... }
  205.      if (LoadString                         { try to load error message... }
  206.                (HInstance, ids_Wrong, Text1, sizeof(Text1)-1) = 0) then
  207.                     lstrcpy (Text1,MsgTxtNotFnd);
  208.      GetWindowText (HWindow, Text2, sizeof(Text2));      {.. get caption.. }
  209.      MessageBox (HWindow, Text1, Text2, MB_IconStop);
  210.      exit;                                       {... and don't end dialog }
  211.      end;   { if no match }
  212. inherited OK (Msg);                          { ok: end dialog }
  213. end;      { OK }
  214.  
  215. {***************************************************************************}
  216.  
  217. {****************************************************************************
  218. *                                                                           *
  219. *           T P a s s W o r d D i a l o g . S e t u p W i n d o w           *
  220. *                                                                           *
  221. *   Does some initialization work for the password entry dialog.            *
  222. *                                                                           *
  223. *   INPUT:  SaverName = Heading of the private section of the saver in      *
  224. *                    CONTROL.INI (something like: 'Screen Saver.savername') *
  225. *                                                                           *
  226. *   OUTPUT: PWProtected = 0 if password for this screen saver is disabled,  *
  227. *                         <> 0 otherwise                                    *
  228. *           CurrentPW   = Current password (both from CONTROL.INI)          *
  229. *                                                                           *
  230. ****************************************************************************}
  231.  
  232. procedure TPassWordDialog.SetupWindow;
  233.  
  234. begin
  235.  
  236. inherited SetupWindow;
  237.  
  238. { read CONTROL.INI, set Enable button accordingly: }
  239. GetProfile (PWProtected, CurrentPW);
  240. CheckDlgButton (HWindow, idc_EnaPWBut, PWProtected);
  241. EnableWindow (GetItemHandle (idc_EnaPWBut), (CurrentPW[0] <> #0));
  242.  
  243. { if there is a current password, enable Old Password edit and static control: }
  244. EnableWindow (GetItemHandle (idc_OldPWLbl), (CurrentPW[0] <> #0));
  245. EnableWindow (GetItemHandle (idc_OldPW), (CurrentPW[0] <> #0));
  246.  
  247. { Limit text length in edit controls to max. password length: }
  248. SendDlgItemMsg (idc_OldPW, EM_LimitText, PWSize, 0);
  249. SendDlgItemMsg (idc_NewPW1, EM_LimitText, PWSize, 0);
  250. SendDlgItemMsg (idc_NewPW2, EM_LimitText, PWSize, 0);
  251.  
  252. end;   { SetupWindow }
  253.  
  254. {****************************************************************************
  255. *                                                                           *
  256. *           T P a s s W o r d D i a l o g . E d i t N o t i f y             *
  257. *                                                                           *
  258. *   Enables "Enable Password" checkbox, depending on New Password edits.    *
  259. *                                                                           *
  260. ****************************************************************************}
  261.  
  262. procedure TPassWordDialog.EditNotify (var Msg: TMessage);
  263. begin
  264. if (Msg.LParamHi = EN_CHANGE) then              { is this a "change" notify? }
  265.    { if so, enable checkbox if any of the passwords isn't blank: }
  266.    EnableWindow (GetItemHandle (idc_EnaPWBut),
  267.      ((CurrentPW[0] <> #0) or
  268.       (SendDlgItemMsg (idc_NewPW1, WM_GetTextLength, 0, 0) > 0)) or
  269.       (SendDlgItemMsg (idc_NewPW2, WM_GetTextLength, 0, 0) > 0));
  270. Msg.Result := 0;
  271. end;   { EditNotify }
  272.  
  273. { two frontends for EditNotify - one for each edit control: }
  274.  
  275. procedure TPassWordDialog.EditNotify1 (var Msg: TMessage);
  276. begin
  277. EditNotify (Msg);
  278. end;
  279.  
  280. procedure TPassWordDialog.EditNotify2 (var Msg: TMessage);
  281. begin
  282. EditNotify (Msg);
  283. end;
  284.  
  285. {****************************************************************************
  286. *                                                                           *
  287. *               T P a s s W o r d D i a l o g . O K                         *
  288. *                                                                           *
  289. *   Ends password entry dialog if all entries are acceptable.               *
  290. *                                                                           *
  291. *   INPUT: CurrentPW = Current password (from CONTROL.INI)                  *
  292. *          SaverName = Heading of the private section of the saver in       *
  293. *                    CONTROL.INI (something like: 'Screen Saver.savername') *
  294. *                                                                           *
  295. ****************************************************************************}
  296.  
  297. procedure TPassWordDialog.OK (var Msg: TMessage);
  298.  
  299. const WriteError: PChar =
  300.         'Couldn''t write CONTROL.INI. Settings probably not saved.';
  301.  
  302. var  OldPWInp, NewPWInp1, NewPWInp2: array [0..PWSize] of char;
  303.      OldInpL, NewInp1L, NewInp2L: integer;
  304.  
  305. begin
  306.  
  307. { read the three edit controls: }
  308. OldInpL  := GetWindowText (GetItemHandle (idc_OldPW), OldPWInp, PWSize+1);
  309. NewInp1L := GetWindowText (GetItemHandle (idc_NewPW1), NewPWInp1, PWSize+1);
  310. NewInp2L := GetWindowText (GetItemHandle (idc_NewPW2), NewPWInp2, PWSize+1);
  311.  
  312. if (OldInpL+NewInp1L+NewInp2L <> 0) then        { all three blank? }
  313.     begin                                       { if not...}
  314.     if (CurrentPW [0] <> #0) then          {..and there is currently a PW.. }
  315.        begin
  316.        EncryptString (OldPWInp);                       { encrypt user input }
  317.        if (lstrcmpi (OldPWInp, CurrentPW) <> 0) then    { check against old }
  318.          begin                                  { error message if no match }
  319.          if (LoadString
  320.                (HInstance, ids_WrongOld, Text1, sizeof(Text1)-1) = 0) then
  321.            lstrcpy (Text1,MsgTxtNotFnd);
  322.          MessageBox (HWindow, Text1, PWDlgCaption, MB_IconStop);
  323.          exit;                                   { don't end dialog }
  324.          end;  { if lstrcmpi }
  325.        end;  { if CurrentPW }
  326.  
  327.     { correct old PW was entered (or currently no PW) }
  328.     if (lstrcmpi (NewPWInp1, NewPWInp2) <> 0) then    { do new PW's match? }
  329.         begin                                       { error message if not }
  330.         if (LoadString
  331.               (HInstance, ids_WrongNew, Text1, sizeof(Text1)-1) = 0) then
  332.           lstrcpy (Text1,MsgTxtNotFnd);
  333.         MessageBox (HWindow, Text1, PWDlgCaption, MB_IconStop);
  334.         exit;                                   { don't end dialog }
  335.         end;   { if lstrcmpi }
  336.  
  337.     { PW entries are ok }
  338.     EncryptString (NewPWInp1);
  339.  
  340.     if not WritePrivateProfileString   { store encrypted new PW in CONTROL.INI }
  341.             (PWStringHeading, PWStringKeyName, NewPWInp1, 'CONTROL.INI') then
  342.        MessageBox (HWindow, WriteError, PWDlgCaption, MB_IconStop);
  343.  
  344.     end; { if all three...}
  345.  
  346. { all PW entries ok - or currently no PW }
  347. Text1 [0] := char ($30 + IsDlgButtonChecked (HWindow, IDC_EnaPWBut));
  348. Text1 [1] := #0;                                { => Text1 := '0' or '1' }
  349. if not WritePrivateProfileString     { write PW-enable-status into CONTROL.INI }
  350.         (SaverName, PWProtKeyName, Text1, 'CONTROL.INI') then
  351.    MessageBox (HWindow, WriteError, PWDlgCaption, MB_IconStop);
  352. WritePrivateProfileString (nil, nil, nil, 'CONTROL.INI'); { flush buffers }
  353.  
  354. inherited OK (Msg);
  355. end;      { OK }
  356.  
  357. {****************************************************************************
  358. *                                                                           *
  359. *                 T P a s s W o r d D i a l o g . H e l p                   *
  360. *                                                                           *
  361. *   Loads and displays help text for password entry dialog.                 *
  362. *                                                                           *
  363. ****************************************************************************}
  364.  
  365. procedure TPassWordDialog.Help (var Msg: TMessage);
  366. begin
  367. if (LoadString (HInstance, ids_PWHelp1, Text1, sizeof(Text1)-1) = 0) then
  368.    exit;
  369. if (LoadString (HInstance, ids_PWHelp2, Text2, sizeof(Text2)-1) = 0) then
  370.    exit;
  371. if (LoadString (HInstance, ids_PWHelp3, Text3, sizeof(Text3)-1) = 0) then
  372.    exit;
  373. lstrcat (lstrcat (Text1,Text2), Text3);
  374. MessageBox (HWindow, Text1, HelpCaption, MB_IconInformation);
  375. end;
  376.  
  377.  
  378. begin
  379. end.
  380.  
  381.